package com.sxbbc.common.core.service.system.pay;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.encrypts.pwd.Encode;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.TransVO;
import com.sxbbc.common.core.entity.jz.BBusiness;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.enums.FundTypeEnums;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.enums.TransTypeEnums;
import com.sxbbc.common.core.service.BBusinessService;
import com.sxbbc.common.core.service.IAccountChangeService;
import com.sxbbc.common.core.service.IAgAccountService;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.service.IInfoParamService;
import com.sxbbc.common.core.service.ITransService;

/**
 * 
 * 转账
 * @author Wahyee
 */
@Service
public class TransServiceImpl implements ITransService {
	
	@Autowired
	private BBusinessService bBusinessService;
	@Autowired
	private IAccountChangeService iAccountChangeService;
	@Autowired
	private ICCustomerService icCustomerService;
	@Autowired
	private IAgAccountService iAgAccountService;
	@Autowired
	private IInfoParamService iInfoParamService;
	
	/**
	 * 
	 * 处理转账 
	 * @param vo 转账信息类 
	 * @param payerId 付款人用户ID
	 * @return
	 * @author Wahyee
	 */
	@Override
	public String handleTrans(TransVO vo,Integer payerId) {
		CalcuUtils calc = CalcuUtils.getInstance();
		if(vo.getCost() == null || vo.getCost().compareTo(BigDecimal.ZERO) <= 0) {
			return "请输入转账金额";
		}
		if(calc.isEquals(vo.getType(), TransTypeEnums.B2C.getId())) {
			//XXX 付款方信息 支付密码统一用用户的支付密码
			CCustomer customer = icCustomerService.getById(payerId);
			if(customer == null) {
				return "用户信息获取失败，请检查网络";
			}
			if(StringUtils.isBlank(customer.getPayWord()) || !Encode.md5Encode(vo.getKey()).equals(customer.getPayWord())) {
				return "支付密码不正确";
			}
			//收款方信息
			Integer payeeId = payerId;
			if(StringUtils.isNotBlank(vo.getPayee())) {
				CCustomer payee = new CCustomer();
				payee.setPhone(vo.getPayee());
				payee = icCustomerService.getCustomer(payee);
				if(payee == null) {
					return "收款人信息获取失败，请联系客服";
				}
				payeeId = payee.getId();
			}
			//转账
			BBusiness business = new BBusiness();
			business.setCid(payerId);
			QueryWrapper<BBusiness> queryWrapper = new QueryWrapper<BBusiness>(business);
			business = bBusinessService.getOne(queryWrapper);
			if(business == null) {
				return "商家信息获取失败，请检查网络";
			}
			this.handleTransFormBusiness(payeeId, business.getId(), vo.getCost(), vo.getFundType());
		}
//		else if(calc.isEquals(vo.getType(), TransTypeEnums.AG_SHARE_TO_MONEY.getId())){
//			//付款方
//			AgAccount account = iAgAccountService.selectByAgid(payerId);
//			if(account == null) {
//				return "代理账户信息获取失败，请检查网络";
//			}
//			//收款方
//			Integer payeeId = payerId;
//			this.handleTransFormAgent(payeeId, payerId, vo.getCost(), vo.getFundType());
//		}
		else{
			return "不支持该业务类型";
		}
		return "";
	}
	
	/**
	 * 
	 * 处理商家转账
	 * @param payeeId 收款人ID
	 * @param bid 商家ID
	 * @param cost 转账金额
	 * @param fundType 资金类型
	 * @author Wahyee
	 * XXX 收款人可优化
	 */
	@Override
	public void handleTransFormBusiness(Integer payeeId,Integer bid,BigDecimal cost,Integer fundType) {
		CalcuUtils calc = CalcuUtils.getInstance();
		String payCode = calc.getOrderCode(HandleTypeEnums.TRANS.getPrefix());
		if(calc.isEquals(fundType, FundTypeEnums.MONEY.getId())) {
			//付款方
			String r = iAccountChangeService.insertBMoneyDetail(bid, HandleTypeEnums.TRANS.getId(), 
					StaticUtils.PAY_OUT, payCode, cost, "转账付款", null);
			if(r.length() > 0) {
				throw new ThrowJsonException(r);
			}
			//收款方
			r = iAccountChangeService.insertCMoneyDetail(payeeId, HandleTypeEnums.TRANS.getId(), 
					StaticUtils.PAY_IN, payCode, cost, "转账收款", null);
			if(r.length() > 0) {
				throw new ThrowJsonException(r);
			}
		}else {
			throw new ThrowJsonException("资金类型不正确");
		}
	}
	

}
